﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Headlight.Model;
using Headlight.Repositories;
using Headlight.Model;

using Headlight.ViewModels;
using Headlight.Controllers.Wizard;
using Headlight.Services;

namespace Headlight.Controllers
{
    public static class VehicleViewButtons
    {
        public const string AddNew = "AddVehicle";
        public const string Next = "Next";
        public const string Cancel = "Cancel";
    }

    public class QuoteController : Controller
    {
        private readonly IQuoteService _quoteService;

        public QuoteController (IQuoteService quoteService)
	    {
            _quoteService = quoteService;
	    }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Vehicle()
        {
            return RedirectToAction("Step");
        }

        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "Secr3t")]
        public ActionResult Index(QuoteViewModel view)
        {
            if (ModelState.IsValid)
            {
                Session["ZipCode"] = view.Quote.ZipCode;
            }

            return RedirectToAction("Vehicle", "Quote");
        }

        [HttpGet]
        public ActionResult Step(int stepNumber, string zip="")
        {
            QuoteViewModel model;
            WizardBuilder wizard;

            if (TempData["wizard"] == null)
            {
                model = new QuoteViewModel();
                wizard = new WizardBuilder(PathType.Full);
            }
            else
            { 
                model = TempData["quote"] as QuoteViewModel;
                wizard = TempData["wizard"] as WizardBuilder;
            }

            TempData.Clear();
            TempData.Add("wizard", wizard);
            TempData.Add("quote", model);
            TempData.Keep("wizard");
            return View(wizard.CurrentStep().ViewName, model);
            //return RedirectToAction("Step", new { stepNumber = 1 });
        }

        [HttpPost]
        public ActionResult NextStep(QuoteViewModel model)
        {
            ActionResult result = null;
            var wizard = TempData["wizard"] as WizardBuilder;

            if (wizard.CurrentStep().ViewType == Wizard.ViewType.Vehicle)
            {
                var buttonClicked = Request.Form["submit-button"];
                switch (buttonClicked)
                {
                    case VehicleViewButtons.AddNew:
                        model.Vehicles.Add(model.Vehicle);
                        result = View(model);
                        break;
                    case VehicleViewButtons.Cancel:
                        result = View(model);
                        break;
                    case VehicleViewButtons.Next:
                        var nextStep = wizard.NextStep();
                        result = RedirectToAction("Step", new { stepNumber=nextStep.Number } );
                        break;
                }
            }
            else
            {
                var nextStep = wizard.NextStep();
                result = RedirectToAction("Step", new { stepNumber = nextStep.Number });
            }

            TempData.Clear();
            TempData["quote"] = model;
            TempData["wizard"] = wizard;
            TempData.Keep("wizard");
            return result;
        }

       #region "Vehicle"
	   [HttpPost]
	   public ActionResult DeleteVehicle(string vehicleId)
	   {
           VehicleRepository rep = new VehicleRepository();
           try
		   {
			   int id = Convert.ToInt32(vehicleId);
               //rep.DeleteVehicle(id);
               rep.DeleteVehicle( rep.Vehicles.Single(v => v.VehicleID.Equals(vehicleId)) );
			   return Json(new { message = "" });
		   }
		   catch(Exception ex)
		   {
			   return Json(new { message = ex.Message });
		   }
	   }
	   #endregion

	   #region "Driver"
	   [HttpPost]
	   public ActionResult DeleteDriver(string driverId)
	   {
		   try
		   {
			   int id = Convert.ToInt32(driverId);
			   //DriverRepository.DeleteDriver(id);
			   return Json(new { message = "" });
		   }
		   catch (Exception ex)
		   {
			   return Json(new { message = ex.Message });
		   }
	   }
	   #endregion

       #region "Results"
       public ActionResult Results()
       {
           return View();
       }
       #endregion

       public ActionResult Final()
       {
           return View();
       }
    }
}
